import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
import http from '../config/request'
import HelloWorld from '@/components/HelloWorld'
import Index from '@/pages/index'
import Login from '../view/login/index.vue'
import Main from '@/components/main'
import organizationAdministration from '../view/organization/organizationAdministration.vue'
import Organization from '@/pages/organization.vue'
import preview from '../view/organization/preview.vue'
import creatSchool from '../view/organization/creatSchool.vue'
import creatStudent from '../view/organization/creatStudent.vue'
import personnelAdministration from '../view/organization/personnelAdministration.vue'
import Test from '@/pages/test'
import ClassSituation from '@/pages/classSituation.vue'

import Outbreaknotice from '@/pages/OutbreakNotice.vue'
import Newsdetail from '@/pages/newsdetail.vue'
import Monitoring from '@/pages/monitoring.vue'
import MonitorDetails from '@/pages/MonitorDetails'

import CardDetail from '@/pages/cardDetail/index.vue'
import Disindectdetail from '@/pages/position/index.vue'
import Classdetail from '@/pages/class.vue'
import editInfo from '@/pages/MonitorDetails/editInfo'
import editTiWenInfo from '@/pages/MonitorDetails/editTiWenInfo'

Vue.use(Router)

const router = new Router({
  routes: [{
    path: '/login',
    name: 'Login',
    component: Login,
    alias: '/'
  }, {
    path: '/index',
    name: 'Index',
    component: Main,
    children: [{
      path: '/index',
      name: 'Index',
      component: () => import('../view/global')
    },
    // 人员监控详情
    {
      path: '/monitorDetails',
      name: 'MonitorDetails',
      component: MonitorDetails
    },
    {
      path: '/disindectdetail',
      name: 'Disindectdetail',
      component: Disindectdetail
    },
    {
      path: '/nation',
      component: () => import('../view/global/nation')
    },
    {
      path: '/province',
      component: () => import('../view/global/province')
    },
    {
      path: '/province/:id',
      props: true,
      component: () => import('../view/global/province')
    },
    {
      path: '/city',
      component: () => import('../view/global/city')
    },
    {
      path: '/city/:id',
      props: true,
      component: () => import('../view/global/city')
    },
    {
      path: '/school',
      component: () => import('../view/global/school')
    },
    {
      path: '/school/:id',
      props: true,
      component: () => import('../view/global/school')
    },
    {
      path: '/class',
      component: () => import('../view/global/class')
    },
    {
      path: '/class/:id',
      props: true,
      component: () => import('../view/global/class')
    },
    {
      path: '/classdetail',
      name: 'Classdetail',
      component: Classdetail
    },
    {
      path: '/hello',
      name: 'HelloWorld',
      component: HelloWorld
    }, {
      path: '/organizationAdministration',
      name: 'organizationAdministration',
      component: organizationAdministration
    }, {

      path: '/organization',
      name: 'Organization',
      component: Organization
    }, {
      path: '/preview',
      name: 'preview',
      component: preview
    }, {
      path: '/creatStudent',
      name: 'creatStudent',
      component: creatStudent
    }, {
      path: '/creatSchool',
      name: 'creatSchool',
      component: creatSchool
    }, {
      path: '/medicalAuthorities',
      name: 'MedicalAuthorities',
      component: () =>
        import('@/pages/medicalAuthorities')
    }, {
      path: '/disinfection',
      name: 'Disinfection',
      component: () =>
        import('@/pages/disinfection')
    }, {
      path: '/antiepidemic',
      name: 'Antiepidemic',
      component: () =>
        import('@/pages/antiepidemic')
    }, {
      path: '/test',
      name: 'Test',
      component: Test
    }, {
      path: '/classSituation',
      name: 'ClassSituation',
      component: ClassSituation
    }, {
      path: '/outbreaknotice',
      name: 'Outbreaknotice',
      component: Outbreaknotice
    },
    {
      path: '/cardDetail',
      name: 'CardDetail',
      component: CardDetail
    },
    {
      path: '/newsdetail',
      name: 'Newsdetail',
      component: Newsdetail
    }, {
      path: '/monitoring',
      name: 'Monitoring',
      component: Monitoring
    }, {
      path: '/personnelAdministration',
      name: 'personnelAdministration',
      component: personnelAdministration
    }, {
      path: '/config/monitor',
      component: () => import('../view/config/monitor')
    }, {
      path: '/config/personal',
      component: () => import('../view/config/personal')
    }, {
      path: '/config/organization',
      component: () => import('../view/config/organization')
    }, {
      path: '/config/orgManage',
      component: () => import('../view/config/orgManage'),
      children: [{
        path: 'list',
        alias: '/',
        component: () => import('../view/config/orgManage/list')
      }, {
        path: 'hospital',
        component: () => import('../view/config/orgManage/hospital')
      }, {
        path: 'hospital/:id',
        props: true,
        component: () => import('../view/config/orgManage/hospital')
      }, {
        path: 'medical',
        component: () => import('../view/config/orgManage/medical')
      }, {
        path: 'medical/:id',
        props: true,
        component: () => import('../view/config/orgManage/medical')
      }]
    }, {
      path: '/config/announcement',
      component: () => import('../view/config/announcement')
    }, {
      path: '/config/announcement/edit',
      component: () => import('../view/config/announcement/edit')
    }, {
      path: '/config/announcement/edit/:id',
      props: true,
      component: () => import('../view/config/announcement/edit')
    }, {
      path: '/config/place',
      component: () => import('../view/config/place'),
      children: [{
        path: 'list',
        alias: '/',
        component: () => import('../view/config/place/list')
      }, {
        path: 'stuck',
        component: () => import('../view/config/place/stuck')
      }, {
        path: 'stuck/:id',
        props: true,
        component: () => import('../view/config/place/stuck')
      }, {
        path: 'tplace',
        component: () => import('../view/config/place/tplace')
      }, {
        path: 'tplace/:id',
        props: true,
        component: () => import('../view/config/place/tplace')
      }]
    }, {
      path: '/emergency/map/province',
      component: () => import('../view/emergency/map/province')
    }, {
      path: '/emergency/map/detail/:id',
      props: true,
      component: () => import('../view/emergency/map/detail')
    }, {
      path: '/info/map',
      props: true,
      component: () => import('../view/info/map')
    }, {
      path: '/emergency/medicalHandover',
      name: 'medicalHandover',
      component: () => import('../view/emergency/medicalHandover')
    },
    {
      path: '/emergency/medical',
      name: 'medicalHandover',
      component: () => import('../view/emergency/medical')
    },
    {
      path: '/editInfo',
      name: 'editInfo',
      component: editInfo
    },
    {
      path: '/editTiWenInfo',
      name: 'editTiWenInfo',
      component: editTiWenInfo
    }
    ]
  }, {
    path: '/screen',
    component: () => import('../view/screen'),
    children: [
      {
        path: 'school',
        component: () => import('../view/screen/School'),
        meta: { full: true, name: 'screen-school' }
      },
      {
        path: 'city',
        component: () => import('../view/screen/City'),
        meta: { full: true, name: 'screen-city' }
      }
    ]
  }]
})

router.beforeEach((to, from, next) => {
  // if (to.matched.length === 0) {
  //     return next('/404');
  // }
  if (to.path === '/login') { // 跳出循环的关键
    next()
    return
  }
  if (!localStorage.token) {
    next('/login')
    return
  }
  if (!store.state.user) {
    http('user/loginindex').then(
      res => {
        if (res.code !== 200) {
          next('/login')
        } else {
          store.commit('login', res.data)
          next()
        }
      }
    )
  } else {
    next()
  }
})

export default router
